package com.sibbay.syncbox.syncdb;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.test.AndroidTestCase;
import android.test.mock.MockContext;

import com.sibbay.syncbox.syncdb.SyncDatabase.SyncConst;

public class SyncDatabaseTest extends AndroidTestCase {

	private static final String TEST_NAME = "$TEST$INSERT$";
	
	private SyncDatabase mSyncDatabase;  
	
	@Override
	protected void setUp() throws Exception {
	    super.setUp();
	    
	    mSyncDatabase = SyncDatabase.getInstance(new TestMockContext(getContext()));
//	    mSyncDatabase = SyncDatabase.getInstance(getContext());
	}
	
	@Override
	protected void tearDown() throws Exception {
	    super.tearDown();
	}
	
	public void testInsertName() {
		final long ret1 = mSyncDatabase.insertName(TEST_NAME, SyncConst.KEY_TYPE_DIR, 100);
		assertEquals("insert id > 0", true, ret1 > 0);
		
	    final int ret2 = mSyncDatabase.deleteName(TEST_NAME);
		assertEquals("delete 1 row", true, ret2 == 1);	// 1 row
	}

	public void testHasName() {
		// don't have name yet
		final boolean ret3 = mSyncDatabase.hasName(TEST_NAME);
		assertEquals("doesn't have name", false, ret3);
		
		final long ret1 = mSyncDatabase.insertName(TEST_NAME, SyncConst.KEY_TYPE_DIR, 100);
		assertEquals("insert id > 0", true, ret1 > 0);
		
		final boolean ret2 = mSyncDatabase.hasName(TEST_NAME);
		assertEquals("has name", true, ret2);
		
	    final int ret4 = mSyncDatabase.deleteName(TEST_NAME);
		assertEquals("delete 1 row", true, ret4 == 1);	// 1 row
	}
	
	///
	
	private static final class TestMockContext extends MockContext {

		final Context mContext;
		
		public TestMockContext(Context context) {
	        mContext = context;
        }

		@Override
		public SQLiteDatabase openOrCreateDatabase(String file, int mode, CursorFactory factory) {
		    return mContext.openOrCreateDatabase("$" + file, mode, factory);
		}
		
		@Override
		public Context getApplicationContext() {
		    return this;	//mContext.getApplicationContext();
		}
	}
 }



